package class141To150;

import java.io.*;
import java.util.Arrays;

public class AVL {

    public static int MAX_N = 100005;

    public static int head;

    public static int cnt;

    public static int[] key = new int[MAX_N];

    public static int[] height = new int[MAX_N];

    public static int[] left = new int[MAX_N];

    public static int[] right = new int[MAX_N];

    public static int[] count = new int[MAX_N];

    public static int[] size = new int[MAX_N];

    public static void up(int i) {
        size[i] = size[left[i]] + size[right[i]] + count[i];
        height[i] = Math.max(height[left[i]], height[right[i]]) + 1;
    }

    public static int leftRotate(int i) {
        int r = right[i];
        right[i] = left[r];
        left[r] = i;
        up(i);
        up(r);
        return r;
    }

    public static int rightRotate(int i) {
        int l = left[i];
        left[i] = right[l];
        right[l] = i;
        up(i);
        up(l);
        return l;
    }

    public static int maintain(int i) {
        int lh = height[left[i]];
        int rh = height[right[i]];
        if (lh - rh > 1) {
            if (height[left[left[i]]] >= height[right[left[i]]]) {
                i = rightRotate(i);
            } else {
                left[i] = leftRotate(left[i]);
                i = rightRotate(i);
            }
        } else if (rh - lh > 1) {
            if (height[right[right[i]]] >= height[left[right[i]]]) {
                i = leftRotate(i);
            } else {
                right[i] = rightRotate(right[i]);
                i = leftRotate(i);
            }
        }
        return i;
    }

    public static void add(int num) {
        head = add(head, num);
    }

    public static int add(int i, int num) {
        if (i == 0) {
            key[++cnt] = num;
            size[cnt] = count[cnt] = height[cnt] = 1;
            return cnt;
        }
        if (key[i] == num) {
            count[i]++;
        } else if (key[i] > num) {
            left[i] = add(left[i], num);
        } else {
            right[i] = add(right[i], num);
        }
        up(i);
        return maintain(i);
    }

    // 查询num的排名，比num小的数字个数+1，就是num的排名
    public static int rank(int num) {
        return small(head, num) + 1;
    }

    // 以i为头的树上，比num小的数字有几个
    public static int small(int i, int num) {
        if (i == 0) {
            return 0;
        }
        if (key[i] >= num) {
            return small(left[i], num);
        } else {
            return size[left[i]] + count[i] + small(right[i], num);
        }
    }

    public static void remove(int num) {
        if (rank(num) != rank(num + 1)) {
            head = remove(head, num);
        }
    }

    public static int removeMostLeft(int i, int mostLeft) {
        if (i == mostLeft) {
            return right[i];
        } else {
            left[i] = removeMostLeft(left[i], mostLeft);
            up(i);
            return maintain(i);
        }
    }

    public static int remove(int i, int num) {
        if (key[i] < num) {
            right[i] = remove(right[i], num);
        } else if (key[i] > num) {
            left[i] = remove(left[i], num);
        } else {
            if (count[i] > 1) {
                count[i]--;
            } else {
                if (left[i] == 0 && right[i] == 0) {
                    return 0;
                } else if (left[i] != 0 && right[i] == 0) {
                    i = left[i];
                } else if (left[i] == 0 && right[i] != 0) {
                    i = right[i];
                } else {
                    int mostLeft = right[i];
                    while (left[mostLeft] != 0) {
                        mostLeft = left[mostLeft];
                    }
                    right[i] = removeMostLeft(right[i], mostLeft);
                    left[mostLeft] = left[i];
                    right[mostLeft] = right[i];
                    i = mostLeft;
                }
            }
        }
        up(i);
        return maintain(i);
    }

    public static int index(int x) {
        return index(head, x);
    }

    public static int index(int i, int x) {
        if (size[left[i]] >= x) {
            return index(left[i], x);
        } else if (size[left[i]] + count[i] < x) {
            return index(right[i], x - size[left[i]] - count[i]);
        }
        return key[i];
    }

    public static int pre(int num) {
        return pre(head, num);
    }

    public static int pre(int i, int num) {
        if (i == 0) {
            return Integer.MIN_VALUE;
        }
        if (key[i] >= num) {
            return pre(left[i], num);
        } else {
            return Math.max(key[i], pre(right[i], num));
        }
    }

    public static int post(int num) {
        return post(head, num);
    }

    public static int post(int i, int num) {
        if (i == 0) {
            return Integer.MAX_VALUE;
        }
        if (key[i] <= num) {
            return post(right[i], num);
        } else {
            return Math.min(key[i], post(left[i], num));
        }
    }

    public static void clear() {
        Arrays.fill(key, 1, cnt + 1, 0);
        Arrays.fill(height, 1, cnt + 1, 0);
        Arrays.fill(left, 1, cnt + 1, 0);
        Arrays.fill(right, 1, cnt + 1, 0);
        Arrays.fill(count, 1, cnt + 1, 0);
        Arrays.fill(size, 1, cnt + 1, 0);
        cnt = 0;
        head = 0;
    }


    public static int n, opt, x;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(System.out);
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            n = (int) in.nval;
            clear();
            for (int i = 1; i <= n; i++) {
                in.nextToken();
                opt = (int) in.nval;
                in.nextToken();
                x = (int) in.nval;
                if (opt == 1) {
                    add(x);
                } else if (opt == 2) {
                    remove(x);
                } else if (opt == 3) {
                    out.println(rank(x));
                } else if (opt == 4) {
                    out.println(index(x));
                } else if (opt == 5) {
                    out.println(pre(x));
                } else if (opt == 6) {
                    out.println(post(x));
                }
            }
        }
        out.flush();
        out.close();
        br.close();
    }
}
